using LabSecurityApi.Models;
using SqlSugar;

namespace LabSecurityApi.Services;

public class UserService : IUserService
{
    private readonly ISqlSugarClient _db;

    public UserService(ISqlSugarClient db)
    {
        _db = db;
    }

    public async Task<bool> CreateAsync(User user)
    {
        user.CreateTime = DateTime.Now;
        user.UpdateTime = DateTime.Now;
        return await _db.Insertable(user).ExecuteCommandAsync() > 0;
    }

    public async Task<bool> DeleteAsync(int id)
    {
        return await _db.Deleteable<User>().In(id).ExecuteCommandAsync() > 0;
    }

    public async Task<bool> UpdateAsync(User user)
    {
        user.UpdateTime = DateTime.Now;
        return await _db.Updateable(user).ExecuteCommandAsync() > 0;
    }

    public async Task<User> GetByIdAsync(int id)
    {
        return await _db.Queryable<User>().InSingleAsync(id);
    }

    public async Task<List<User>> GetAllAsync()
    {
        return await _db.Queryable<User>().ToListAsync();
    }
} 